#include <fstream>
#include <random>
#include <string>
using namespace std;
//argv[1]-> 数据来源，1:poj.txt  2:hdu.txt
//argv[2]-> 插入次数
//argv[3]-> 查询次数
//argv[4]-> 特殊特征的构造  1:全中文
//argv[5]-> 输出.out文件名

class Pair
{
public:
    string key;
    int value;
};

Pair allPair[620000];
int pairPtr = -1;

int main(int argc, char *argv[])
{
    ifstream fin;
    string inName = argv[5];
    inName += ".in";
    ofstream finput(inName, ios::out);
    string stdName = argv[5];
    stdName += "_Answer.out";
    ofstream fstandout(stdName, ios::out);
    if (argv[1][0] == '1')
    {
        fin = ifstream("poj.txt", ios::in);
    }
    else if (argv[1][0] == '2')
    {
        fin = ifstream("hdu.txt", ios::in);
    }
    int insertNum = stoi(argv[2]);
    int queryNum = stoi(argv[3]);
    char line[100];
    random_device rd;
    if (argv[4][0] == '1')
    {
        while (insertNum > 0 && fin.getline(line, 100))
        {
            int i = 0;
            //得到key的开始位置
            for (;; i++)
            {
                if (line[i] == ' ')
                {
                    i++;
                    break;
                }
            }
            unsigned char a = line[i];
            if (a >> 7 == 1) //key值是汉字
            {
                char key[50];
                int value = 0;
                //得到value的开始位置,复制key
                for (int j = 0;; j++)
                {
                    if (line[i] == ' ')
                    {
                        key[j] = '\0';
                        i++;
                        break;
                    }
                    else
                    {
                        key[j] = line[i];
                        i++;
                    }
                }
                //复制value
                for (;; i++)
                {
                    if (line[i] == ' ')
                        break;
                    value = value * 10 + line[i] - 48;
                }
                bool noRepeat = true;
                for (int k = 0; k < pairPtr; k++)
                {
                    if (allPair[k].key.compare(key) == 0)
                    {
                        noRepeat = false;
                        break;
                    }
                }
                if (noRepeat)
                {
                    finput << "0 " << key << " " << value << endl;
                    pairPtr++;
                    insertNum--;
                    allPair[pairPtr].key = key;
                    allPair[pairPtr].value = value;
                }
            }
            // for(int i=0;i<100;i++)
            // {
            //     line[i]=0;
            // }
        }
        while (queryNum > 0)
        {
            int index = rd() % pairPtr;
            if (index % 7 == 0) //制造查找失败
            {
                int len = allPair[index].key.size();
                string substr = allPair[index].key.substr(0, len - 1);
                char a = allPair[index].key.at(len - 1) + 1;
                finput << "1 " << substr << a << endl;
                fstandout << "-1" << endl;
                queryNum--;
            }
            finput << "1 " << allPair[index].key << endl;
            fstandout << allPair[index].value << endl;
            queryNum--;
        }
        finput << "2";
    }
    else
    {
        while (insertNum > 0 && fin.getline(line, 100))
        {
            if (rd() % 3 == 1)
            {
                int i = 0;
                char key[100];
                int value = 0;
                for (;; i++)
                {
                    if (line[i] == ' ')
                    {
                        i++;
                        break;
                    }
                }
                for (int j = 0;; j++)
                {
                    if (line[i] == ' ')
                    {
                        key[j] = '\0';
                        i++;
                        break;
                    }
                    else
                    {
                        key[j] = line[i];
                        i++;
                    }
                }
                for (;; i++)
                {
                    if (line[i] == ' ')
                        break;
                    value = value * 10 + line[i] - 48;
                }
                bool noRepeat = true;
                for (int k = 0; k < pairPtr; k++)
                {
                    if (allPair[k].key.compare(key) == 0)
                    {
                        noRepeat = false;
                        break;
                    }
                }
                if (noRepeat)
                {
                    finput << "0 " << key << " " << value << endl;
                    pairPtr++;
                    insertNum--;
                    allPair[pairPtr].key = key;
                    allPair[pairPtr].value = value;
                }
            }
            // for(int i=0;i<100;i++)
            // {
            //     line[i]=0;
            // }
        }
        while (queryNum > 0)
        {
            int index = rd() % pairPtr;
            if (index % 7 == 0) //制造查找失败
            {
                int len = allPair[index].key.size();
                string substr = allPair[index].key.substr(0, len - 1);
                char a = allPair[index].key.at(len - 1) + 1;
                finput << "1 " << substr << a << endl;
                fstandout << "-1" << endl;
                queryNum--;
            }
            finput << "1 " << allPair[index].key << endl;
            fstandout << allPair[index].value << endl;
            queryNum--;
        }
        finput << "2";
    }
    return 0;
}